import os


class GlobalSetting:
    config_name = './config'

    # 设置值
    log_dir = os.getcwd()
    log_output_dataset = 0
    log_output_result = 0
    rcg_dir = os.getcwd()
    rcg_save_folder = os.getcwd()

    def read_file(self):
        """从文件读取设置

        Returns:
        """
        # 判断配置文件是否存在，存在则读取，不存在创建
        if os.path.isfile(self.config_name):
            with open(self.config_name, 'r') as f_read:
                lines = f_read.readlines()

            for line in lines:
                # 删首尾空
                line = line.strip()
                # 此行无内容
                if len(line) == 0:
                    continue

                line_info = line.split('=')
                # '='号后无内容
                if len(line_info) == 1:
                    continue
                # 获取值
                value = line_info[1].strip(' \'\"')

                # 对属性进行判断
                if line[0:7] == 'log_dir':
                    self.log_dir = value
                elif line[0:7] == 'rcg_dir':
                    self.rcg_dir = value
                elif line[0:15] == 'rcg_save_folder':
                    self.rcg_save_folder = value
                elif line[0:18] == 'log_output_dataset':
                    self.log_output_dataset = int(value)
                elif line[0:17] == 'log_output_result':
                    self.log_output_result = int(value)
        else:
            self.save_file()

    def save_file(self):
        """将设置保存到文件

        Returns:

        """
        with open(self.config_name, 'w') as f_write:
            f_write.write('log_dir=' + self.log_dir + '\n')
            f_write.write('log_output_dataset=' + str(self.log_output_dataset) + '\n')
            f_write.write('log_output_result=' + str(self.log_output_result) + '\n')
            f_write.write('rcg_dir=' + self.rcg_dir + '\n')
            f_write.write('rcg_save_folder=' + self.rcg_save_folder + '\n')

        print('Save successfully')
        return


# 全局配置
global_setting = GlobalSetting()
